<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\ConfigEnum;
use app\enum\KuaiShouEnum;
use app\model\KsProjectBidTaskLogModel;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class KuaiShouProjectBidListWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('kuai_shou_project_bid_list_work');
        $this->setDescription('批量入队快手项目出价');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=======批量推送快手项目出价开始' . date('Y-m-d H:i:s', time()) . '=======');
        $st = time() + 300;
        $redis = Cache::store('redis')->handler();
        $key = ConfigEnum::CACHE_CONFIG[KuaiShouEnum::BATCH_CONFIG_KEY]['project_bid_push'];

        (new KsProjectBidTaskLogModel())->where('status', '=', KsProjectBidTaskLogModel::STATUS_WAIT)
            ->chunk(100, function ($items) use ($st, $redis, $key, $output) {
                if ($items) {
                    foreach ($items as $item) {
                        if ($item['id']) {
                            $redis->rPush($key, $item['id']);
                            $output->writeln('=======批量入队快手项目出价[' . $item['id'] . ']成功' . date('Y-m-d H:i:s', time()) . '=======');
                        }
                    }
                } else {
                    return false;
                }
                if ($st <= time() - 300) {
                    return false;
                }
            }, 'id', 'DESC');
        $output->writeln('=======批量入队快手项目出价结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
